<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="paramsRules" size="small" :inline="true" label-width="80px">
      <el-form-item label="编号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入编号"/>
      </el-form-item>
      <el-form-item label="用户ID" prop="idCode">
        <el-input v-model="queryParams.idCode" placeholder="请输入用户ID" clearable/>
      </el-form-item>
      <el-form-item label="WX号" prop="wxCode">
        <el-input v-model="queryParams.wxCode" placeholder="请输入WX号" clearable/>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable/>
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-input v-model="queryParams.startTime" placeholder="请输入开始时间" clearable/>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input v-model="queryParams.endTime" placeholder="请输入结束时间" clearable/>
      </el-form-item>
<!--      <el-form-item label="原始金额" prop="originalMoney">
        <el-input v-model="queryParams.originalMoney" placeholder="请输入原始金额" clearable/>
      </el-form-item>-->
      <el-form-item label="总收金额" prop="totalInMoney">
        <el-input v-model="queryParams.totalInMoney" placeholder="请输入总收金额" clearable/>
      </el-form-item>
      <el-form-item label="总支金额" prop="totalOutMoney">
        <el-input v-model="queryParams.totalOutMoney" placeholder="请输入总支金额" clearable/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryWxData">检索回填</el-button>
        <el-button type="success" icon="el-icon-search" size="mini" :disabled="!(randomData.inValueList && randomData.outValueList)" @click="createWxPdf">生成文件
        </el-button>
      </el-form-item>
    </el-form>
    <div style="clear: both"></div>
    <div ref="showDetailDiv" v-if="randomData.inValueList && randomData.outValueList && randomData.totalDiffMoney">
      <span>处理后总额(收)：<span style="color: green; margin-right: 10px">{{randomData.dealInTotalMoney }};</span> </span>
      <span>处理后总额(支)：<span style="color: red; margin-right: 10px">{{randomData.dealOutTotalMoney }};</span> </span>
      <span>差值：<span style="color: orange">{{randomData.totalDiffMoney }};</span> </span>
    </div>
    <div style="margin-top: 8px" v-if="randomData.inValueList && randomData.outValueList">
      <el-tabs v-model="activeName" tab-position="top" type="border-card" @tab-click="handleClick">
        <el-tab-pane :label="'收入明细 (' + randomData.inValueList.length + ')'" name="in">
          <el-table v-loading="editLoading" :data="randomData.inValueList" :cell-class-name="tableCellClassName"
                    @row-dblclick="dbclick">
            <el-table-column label="交易单号" align="center" prop="transactionSummary">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionSummary"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionSummary }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易日期" align="center" prop="transactionDate">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionDate"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionDate }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易类型" align="center" prop="transactionChannels">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionChannels"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionChannels }}</span>
              </template>
            </el-table-column>
            <el-table-column label="收/支/其他" align="center" prop="logNo">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.logNo"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.logNo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易方式" align="center" prop="currentBalance">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.currentBalance"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.currentBalance }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易金额" align="center" prop="transactionMoneyInt">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionMoneyInt"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionMoneyInt }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易对方" align="center" prop="oppositionInformation">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.oppositionInformation"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.oppositionInformation }}</span>
              </template>
            </el-table-column>
            <el-table-column label="商户单号" align="center" prop="transactionNote">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionNote"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionNote }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane :label="'支出明细 (' + randomData.outValueList.length + ')'" name="out">
          <el-table v-loading="editLoading" :data="randomData.outValueList" :cell-class-name="tableCellClassName"
                    @row-dblclick="dbclick">
            <el-table-column label="交易单号" align="center" prop="transactionSummary">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionSummary"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionSummary }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易日期" align="center" prop="transactionDate">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionDate"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionDate }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易类型" align="center" prop="transactionChannels">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionChannels"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionChannels }}</span>
              </template>
            </el-table-column>
            <el-table-column label="收/支/其他" align="center" prop="logNo">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.logNo"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.logNo }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易方式" align="center" prop="currentBalance">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.currentBalance"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.currentBalance }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易金额" align="center" prop="transactionMoneyInt">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionMoneyInt"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionMoneyInt }}</span>
              </template>
            </el-table-column>
            <el-table-column label="交易对方" align="center" prop="oppositionInformation">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.oppositionInformation"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.oppositionInformation }}</span>
              </template>
            </el-table-column>
            <el-table-column label="商户单号" align="center" prop="transactionNote">
              <template slot-scope="scope">
                <el-input
                  v-if="scope.row.index + ',' + scope.column.index == currentCell"
                  :ref="scope.row.index + ',' + scope.column.index"
                  v-model="scope.row.transactionNote"
                  @blur="hideInput(scope.row)"
                />
                <span v-else>{{ scope.row.transactionNote }}</span>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>
</template>

<script>
import {getWxDetailList, createWxPdf} from "@/api/system/file";

export default {
  name: 'wxPdf',
  data() {
    return {
      queryParams: {
        code: null,
        idCode: null,
        wxCode: null,
        userName: null,
        typeId: null,
        startTime: null,
        endTime: null,
        originalMoney: null,
        totalInMoney: null,
        totalOutMoney: null,
        createTime: null
      },
      paramsRules: {
        code: [{required: true, trigger: "blur", message: "编号不能为空!"}],
        idCode: [{required: true, trigger: "blur", message: "用户ID不能为空!"}],
        wxCode: [{required: true, trigger: "blur", message: "WX号不能为空!"}],
        userName: [{required: true, trigger: "blur", message: "用户名不能为空!"}],
        startTime: [{required: true, trigger: "blur", message: "开始时间不能为空!"}],
        endTime: [{required: true, trigger: "blur", message: "结束时间不能为空!"}],
        originalMoney: [{required: true, trigger: "blur", message: "原始金额不能为空!"}],
        totalInMoney: [{required: true, trigger: "blur", message: "总收金额不能为空!"}],
        totalOutMoney: [{required: true, trigger: "blur", message: "总支金额不能为空!"}]
      },
      randomData: {},
      editLoading: false,
      activeName: 'in',
      currentCell: null,
    }
  },
  methods: {
    // 表单重置
    reset() {
      this.queryParams = {
        code: null,
        idCode: null,
        wxCode: null,
        userName: null,
        typeId: null,
        startTime: null,
        endTime: null,
        originalMoney: null,
        totalInMoney: null,
        totalOutMoney: null,
        totalDiffMoney: null,
        createTime: null
      };
      this.resetForm("queryForm");
    },
    // 查询检索数据
    queryWxData() {
      this.$refs["queryForm"].validate(valid => {
        if (valid) {
          getWxDetailList(this.queryParams).then(res => {
            this.randomData = res.data;
          })
        }
      })
    },
    // 生成pdf文件
    createWxPdf() {
      createWxPdf(this.randomData).then(res => {
        this.$emit('createPdfResp', res);
      })
    },
    // 返回顶部
    scrollToTop() {
      this.$refs.showDetailDiv.scrollIntoView({behavior: 'smooth'});
    },
    /* 展示表格处理方法 */
    // 改变tab页
    handleClick(tab, event) {
      console.log(tab, event);
      this.activeName = tab.name;
      this.hideInput(this.randomData.inValueList[0]);
      this.hideInput(this.randomData.outValueList[0]);
    },
    // 关闭编辑状态
    hideInput(row) {
      this.currentCell = null
      this.calcTotalMoney();
    },
    calcTotalMoney() {
      let inTotal = this.randomData.inValueList.reduce((sum, e) => sum + Number(e.transactionMoneyInt || 0), 0);
      let outTotal = this.randomData.outValueList.reduce((sum, e) => sum + Number(e.transactionMoneyInt || 0), 0);
      this.$set(this.randomData, 'dealInTotalMoney', inTotal);
      this.$set(this.randomData, 'dealOutTotalMoney', Math.abs(outTotal));
      this.$set(this.randomData, 'totalDiffMoney', inTotal - Math.abs(outTotal));
    },
    // 给单元格绑定横向和竖向的index，这样就能确定是哪一个单元格
    tableCellClassName({row, column, rowIndex, columnIndex}) {
      row.index = rowIndex;
      column.index = columnIndex;
    },
    // 获得当前双击的单元格的横竖index，然后拼接成一个唯一字符串用于判断，并赋给currentCell
    // 拼接后类似这样："1,0","1,1",
    dbclick(row, column) {
      this.currentCell = row.index + ',' + column.index;
      // input 自动获取焦点
      // 双击后自动获得焦点
      this.$nextTick(() => {
        this.$refs[row.index + ',' + column.index].focus()
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
